package com.example.cunion.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.cunion.entity.Operation;
import com.example.cunion.exception.CunionException;
import com.example.cunion.mapper.OperationMapper;
import com.example.cunion.service.OperationService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
* @author Aero
* @description 针对表【operation】的数据库操作Service实现
* @createDate 2023-09-29 17:41:08
*/
@Service
public class OperationServiceImpl extends ServiceImpl<OperationMapper, Operation>
    implements OperationService {

    @Resource
    private OperationMapper operationMapper;
   @Override
    public ArrayList<HashMap> searchAllOperation() {
        // 调用operationMapper的searchAllOperation()方法，获取所有操作信息
        ArrayList<HashMap> list = operationMapper.searchAllOperation();
        return list;
    }

    @Override
    public Integer addOperationByMp(Operation operation) {
        int insert = operationMapper.insert(operation);
        if (insert != 1){
            throw new CunionException("添加失败");
        }
        return insert;
    }

    @Override
    public Integer deletedOperationByMp(String operationId) {
        int result = operationMapper.deleteById(operationId);
        if (result != 1){
            throw new CunionException("删除失败！");
        }
        return result;
    }

    @Override
    public Integer updateOperationByMp(Operation operation) {
        int result = operationMapper.updateById(operation);
        if (result != 1){
            throw new CunionException("修改失败");
        }
        return result;
    }

    @Override
    public List<Operation> searchOperationByMp(Integer start, Integer length) {
        Page<Operation> page = new Page<>(start, length);
        QueryWrapper<Operation> QueryWrapper = new QueryWrapper<>();
        Page<Operation> selectPage = operationMapper.selectPage(page, QueryWrapper);
        List<Operation> list = selectPage.getRecords();
        return list;
    }
}




